package cn.b.sky.mes.hr.controller;

import java.util.List;

import cn.b.sky.mes.hr.model.ShiftingPermission;
import cn.b.sky.mes.hr.model.condition.ShiftingPermissionCondition;
import cn.b.sky.mes.hr.service.ShiftingPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.common.model.TreeInterface;
import cn.b.sky.common.utils.TreeUtil;
import cn.b.sky.user.controller.SecurityController;

import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping(value = "/shiftingPermission/*")
public class ShiftingPermissionController extends SecurityController {
  private static final String SHIFTING_PERMISSION_LIST_PAGE = "hr/shiftingPermission/shiftingPermission_list";

  private static final String SHIFTING_PERMISSION_ADD_PAGE = "hr/shiftingPermission/shiftingPermission_add";

  @Autowired
  private ShiftingPermissionService shiftingPermissionService;

  @Autowired
  private WorkCenterService workCenterService;

  /**
   * 
   * <strong>Title : shiftingPermissionListPage<br>
   * </strong> <strong>Description : </strong>权限列表页面映射<br>
   * <strong>Create on : 2016年3月25日 上午10:29:10<br>
   * </strong>
   * <p>
   * 
   * @param navTabId
   * @param model
   * @return String
   * @throws
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("shiftingPermission_list")
  public String shiftingPermissionListPage(String navTabId, Model model) {
    bindWorcenterTree(navTabId, model, null);
    return SHIFTING_PERMISSION_LIST_PAGE;
  }

  /**
   * 
   * <strong>Title : addShiftingPermissionPage<br>
   * </strong> <strong>Description : </strong>新增页面映射<br>
   * <strong>Create on : 2016年3月25日 上午10:29:29<br>
   * </strong>
   * <p>
   * 
   * @param navTabId
   * @param model
   * @return String
   * @throws
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("shiftingPermission_add")
  public String addShiftingPermissionPage(String navTabId, Model model) {
    bindWorcenterTree(navTabId, model, null);
    return SHIFTING_PERMISSION_ADD_PAGE;
  }

  /**
   * 
   * <strong>Title : bindWorcenterTree<br>
   * </strong> <strong>Description : </strong>绑定工作中心的tree<br>
   * <strong>Create on : 2016年3月25日 上午10:29:52<br>
   * </strong>
   * <p>
   * 
   * @param navTabId
   * @param model
   *            void
   * @throws
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  private void bindWorcenterTree(String navTabId, Model model, Integer lockFlag) {
    List<WorkCenter> workCenters = workCenterService.getAllWorkCenterListBySiteCode(getSiteCode(), lockFlag);
    List<TreeInterface> workCentersTree = TreeUtil.gradingRecursion(
        workCenters.toArray(new WorkCenter[workCenters.size()]), null);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("workCentersTree", workCentersTree);
  }

  /**
   * 
   * <strong>Title : getShiftingPermissionList<br>
   * </strong> <strong>Description : </strong>获取权限列表数据<br>
   * <strong>Create on : 2016年3月25日 上午10:30:08<br>
   * </strong>
   * <p>
   * 
   * @param condition
   * @return DWZResult
   * @throws
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("getShiftingPermissionList")
  @ResponseBody
  public DWZResult getShiftingPermissionList(String workcenterCodes, ShiftingPermissionCondition condition) {
    if (!StringUtils.isEmpty(workcenterCodes)) {
      condition.setWorkcenterCodes(workcenterCodes.split(","));
    }
    condition.setSiteCode(getSiteCode());
    List<ShiftingPermission> list = shiftingPermissionService.getShiftingPermissionsListByCondition(condition);
    int count = shiftingPermissionService.getShiftingPermissionsCountByCondition(condition);
    condition.setTotalCount(count);
    condition.setData(list);
    return ajaxDone(condition);
  }

  /**
   * 
   * <strong>Title : unexistPermissionByWorkcenterAndUser<br>
   * </strong> <strong>Description : </strong>判断工作中心不存在该用户的权限<br>
   * <strong>Create on : 2016年3月25日 上午10:30:28<br>
   * </strong>
   * <p>
   * 
   * @param workcenterCode
   * @param userCode
   * @return boolean
   * @throws
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("unexistPermissionByWorkcenterAndUser")
  @ResponseBody
  public boolean unexistPermissionByWorkcenterAndUser(String workcenterCode, String userCode) {
    if (!StringUtils.isEmpty(userCode) && !StringUtils.isEmpty(workcenterCode)) {
      return null == shiftingPermissionService.getPermissionsByWorkcenterAndUser(getSiteCode(), workcenterCode,
          userCode, null);
    }
    return false;
  }

  /**
   * 
   * <strong>Title : saveShiftingPermissions<br>
   * </strong> <strong>Description : </strong>保存权限数据<br>
   * <strong>Create on : 2016年3月25日 上午10:30:54<br>
   * </strong>
   * <p>
   * 
   * @param shiftingPermissions
   * @param navTabId
   * @return DWZResult
   * @throws
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("saveShiftingPermissions")
  @ResponseBody
  public DWZResult saveShiftingPermissions(String shiftingPermissions, String navTabId) {
    List<ShiftingPermission> permissions = JSONArray.parseArray(shiftingPermissions, ShiftingPermission.class);
    for (ShiftingPermission shiftingPermission : permissions) {
      shiftingPermission.setSiteCode(getSiteCode());
      setCreateInfo(shiftingPermission);
      shiftingPermissionService.insertShiftingPermission(shiftingPermission);
    }
    return navTabAjaxDone(navTabId, null, null, null,
        getImeViewMessageI18n(null, "successfulOperation.M"),
        null);
  }

  /**
   * 
   * <strong>Title : deleteShiftingPermissions<br>
   * </strong> <strong>Description : </strong>删除权限数据<br>
   * <strong>Create on : 2016年3月25日 上午10:31:06<br>
   * </strong>
   * <p>
   * 
   * @param shiftingPermissions
   * @param navTabId
   * @return DWZResult
   * @throws
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("deleteShiftingPermissions")
  @ResponseBody
  public DWZResult deleteShiftingPermissions(String shiftingPermissions, String navTabId) {
    List<ShiftingPermission> permissions = JSONArray.parseArray(shiftingPermissions, ShiftingPermission.class);
    for (ShiftingPermission shiftingPermission : permissions) {
      shiftingPermission.setSiteCode(getSiteCode());
      shiftingPermissionService.deleteShiftingPermission(shiftingPermission);
    }
    return navTabAjaxDone(navTabId, null, null, null,
        getImeViewMessageI18n(null, "successfulOperation.M"),
        null);
  }

}
